package com.cqh.recruit.controller.school;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqh.recruit.pojo.dto.LActivityCheckQueryDto;
import com.cqh.recruit.pojo.entity.ActivityCheck;
import com.cqh.recruit.pojo.vo.LActivityCheckListVo;
import com.cqh.recruit.result.ErrorCode;
import com.cqh.recruit.result.Result;
import com.cqh.recruit.service.IActivityCheckService;
import com.cqh.recruit.utils.EmployeeThreadLocal;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Description:
 * @Author: DengHao
 * @Date: 2025/10/18 18:49
 */
@RestController()
@RequestMapping("/api/school/check")
public class ActivityCheckController {


    @Autowired
    private IActivityCheckService activityCheckService;
    @GetMapping("/find")
    @Operation(summary = "根据id查询审核情况")
    public Result find(Long id) {
        if (id == null) {
            return Result.buildFail(2001, "id不能为空");
        }
        LActivityCheckListVo aCheck = activityCheckService.findActivityCheckById(id);
        return Result.buildSuccess(aCheck);
    }

    @PostMapping("/add")
    @Operation(summary = "添加")
    public Result add(@RequestBody ActivityCheck  aCheck) {
        aCheck.setCreateTime(LocalDateTime.now());
        activityCheckService.save(aCheck);
        return Result.buildSuccess();
    }
    @PostMapping("/update")
    @Operation(summary = "更新")
    public Result update(@RequestBody ActivityCheck aCheck) {
        try {
            System.out.println("收到审核更新请求:");
            System.out.println("- 审核ID: " + aCheck.getId());
            System.out.println("- 活动ID: " + aCheck.getActivityId());
            System.out.println("- 审核状态: " + aCheck.getStatus());
            System.out.println("- 审核意见: " + aCheck.getRemark());
            
            aCheck.setUpdateTime(LocalDateTime.now());
            
            // 如果是审核通过操作，需要检查活动人数限制
            if (aCheck.getStatus() != null && aCheck.getStatus() == 1) {
                System.out.println("开始检查活动人数限制...");
                Result capacityCheckResult = activityCheckService.checkAndUpdateActivityCapacity(aCheck);
                if (capacityCheckResult.getCode() != 0) {
                    System.out.println("人数检查失败: " + capacityCheckResult.getMsg());
                    // 人数已满，自动设置为审核不通过
                    aCheck.setStatus(2);
                    aCheck.setRemark("活动人数已满，审核不通过");
                    activityCheckService.updateById(aCheck);
                    return capacityCheckResult;
                }
                System.out.println("人数检查通过，继续审核流程");
            }
            
            // 正常更新审核状态
            activityCheckService.updateById(aCheck);
            return Result.buildSuccess();
        } catch (Exception e) {
            return Result.buildFail(ErrorCode.SYSTEM_ERROR, "审核更新失败：" + e.getMessage());
        }
    }
    @GetMapping("/delete")
    @Operation(summary = "删除")
    public Result delete(Long id) {
        if (id == null) {
            return Result.buildFail(2001, "id不能为空");
        }
        activityCheckService.removeById(id);
        return Result.buildSuccess();
    }
    @PostMapping("/list")
    @Operation(summary = "查询审核列表")
    public Result list(@RequestBody LActivityCheckQueryDto lActivityCheckQueryDto) {
        Page<LActivityCheckListVo> resultPage = activityCheckService.findLActivityCheckByQueryDto(lActivityCheckQueryDto);
        return Result.buildSuccess(resultPage.getRecords(), resultPage.getTotal());
    }
}
